﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace JEdwonTune.Lib.Jskit {
    public class JsParser {
        public static String ParseJsFile2Zipped(String fileName, String filePath) {
            String[] lines = File.ReadAllLines(filePath);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < lines.Length; i++) {
                String tmp = lines[i];
                tmp = tmp.Replace("\t", " ");
                //去掉首尾空格
                tmp = tmp.Trim();
                //去掉单行的注释
                if (tmp.IndexOf("//") == 0) continue;


                //查找在引号内的"//"
                String pattern = "\"([^\"]*)\"";
                Match match = Regex.Match(tmp, pattern);
                if (match != null && match.Groups.Count == 2) {
                    String v = match.Groups[1].Value;
                    if (!String.IsNullOrEmpty(v)) {
                        tmp = tmp.Replace(v, v.Replace("//", "{||}"));

                    }
                }
                pattern = "'([^']*)'";
                match = Regex.Match(tmp, pattern);
                if (match != null && match.Groups.Count == 2) {
                    String v = match.Groups[1].Value;
                    if (!String.IsNullOrEmpty(v)) {
                        tmp = tmp.Replace(v, v.Replace("//", "{||}"));

                    }
                }

                //处理单行注释
                pattern = "[^/]*(//.*)$";
                match = Regex.Match(tmp, pattern);
                if (match != null && match.Groups.Count == 2) {
                    tmp = tmp.Replace(match.Groups[1].Value, "");
                }

                //处理js的/xxx/正则表达式
                pattern = "(/)[^\\*][^/]*[^\\*](/)";
                match = Regex.Match(tmp, pattern);
                if (match != null && match.Groups.Count == 3) {
                    tmp = tmp.Replace(match.Groups[1].Value, "{REGEX1}");
                    tmp = tmp.Replace(match.Groups[2].Value, "{REGEX2}");
                }
                tmp = tmp.Trim();

                //去掉换行回车
                tmp = tmp.Replace("\r\n", "");
                tmp = tmp.Replace("\r", "");
                tmp = tmp.Replace("\n", "");
                sb.Append(tmp);
            }
            String content = sb.ToString();
            //将*/换成特别字符
            content = content.Replace("*/", "`");
            while (Regex.IsMatch(content, "/\\*[^`]*`")) {
                content = Regex.Replace(content, "/\\*[^`]*`", "");
            }
            //去掉等号两边的空格
            content = Regex.Replace(content, "[ ]*=[ ]*", "=");
            //去掉等号两边的空格
            content = Regex.Replace(content, "[ ]*==[ ]*", "==");
            //去掉等号两边的空格
            content = Regex.Replace(content, "[ ]*===[ ]*", "===");
            //去掉等号两边的空格
            content = Regex.Replace(content, "[ ]*&&[ ]*", "&&");
            //去掉等号两边的空格
            content = Regex.Replace(content, "[ ]*\\|\\|[ ]*", "||");
            //去掉等号两边的空格
            content = Regex.Replace(content, "[ ]*!=[ ]*", "!=");
            //去掉等号两边的空格
            content = Regex.Replace(content, "[ ]*!==[ ]*", "!==");
            //去掉加号两边的空格
            content = Regex.Replace(content, "[ ]*\\+[ ]*", "+");
            //去掉冒号两边的空格
            content = Regex.Replace(content, "[ ]*:[ ]*", ":");
            //去掉else两边的空格
            content = Regex.Replace(content, "\\}[ ]*else[ ]*\\{", "}else{");
            //将://替换回来
            content = content.Replace("`", "*/");
            content = content.Replace("{REGEX1}", "/");
            content = content.Replace("{REGEX2}", "/");
            content = content.Replace("{||}", "//");

            return content;
        }
    }
}
